package com.imu.purse.api.controller.trade;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.imu.common.exception.ErrorDesc;
import com.imu.common.exception.ErrorUtil;
import com.imu.common.utils.R;
import com.imu.purse.api.controller.ApiAbstractController;
import com.imu.purse.modules.trade.service.GradeInfoService;
import com.imu.security.SessionTokenProviderFactory;
import com.xiaoleilu.hutool.util.StrUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 俱乐接口
 * @author sunhongwei
 * @email sunhongwei@gmail.com
 * @date 2018-03-22 17:27
 */
@RestController
@RequestMapping("/api/club")
@Api(tags="俱乐部接口")
public class ApiClubController extends ApiAbstractController
{
	@PostMapping("relegate")
    @ApiOperation(value = "俱乐部降级", notes = "俱乐部降级说明")
    @ApiImplicitParams({
    	@ApiImplicitParam(paramType = "header", dataType="String",    name = "X-IMU-SECRET",  value = "密钥",  required = true),
    	@ApiImplicitParam(paramType = "header", dataType="String",    name = "X-IMU-SESSION", value = "会话",  required = true)
    })
    public R relegate(HttpServletRequest request, @RequestHeader("X-IMU-SECRET") String secret, @RequestHeader("X-IMU-SESSION") String token) 
	{
        //判断Session 是否存在
        String uid = SessionTokenProviderFactory.getSessionTokenProvider().getUserId(secret, token);
        if (StrUtil.isBlank(uid)) {
            return ErrorUtil.errorMesg(request, ErrorDesc.SESSION_EMPTY);
        }
        
        service.grade("club:" + uid, uid);
        return R.ok(1);
    }
    

	@Autowired
	private GradeInfoService service;
}
